/*
 * Copyright (c) 2010-2011 NOO. All Rights Reserved.
 * [Id:FilterUtils.java  2011-11-06 下午2:35 poplar.yfyang ]
 */
package org.mumu.pawn.fm.util;

import com.google.common.collect.Lists;
import org.mumu.pawn.fm.annotation.OrderByColumn;
import org.richfaces.component.SortOrder;

import java.lang.reflect.Field;
import java.util.List;

/**
 * <p>
 * 排序工具类.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2011-11-06 下午2:35
 * @since JDK 1.5
 */
public class SortUtils {
    /**
     * 扫描指定实体类下的需要进行过滤的字段名称。
     *
     * @param cls 指定实体类
     * @return 需要排序的字段名称和排序
     */
    public static List<OrderField> getFilterColumn(Class<?> cls) {
        //获取所有的属性
        Field[] fields = cls.getDeclaredFields();
        List<OrderField> orderFields = Lists.newArrayList();
        for (Field field : fields) {
            if (field.isAnnotationPresent(OrderByColumn.class)) {
                SortOrder sto = field.getAnnotation(OrderByColumn.class).order();
                orderFields.add(new OrderField(field.getName(), sto));
            }
        }
        return orderFields;
    }
}
